package cn.tedu.ivos.user.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.user.mapper.UserMapper;
import cn.tedu.ivos.user.pojo.dto.UserLoginParam;
import cn.tedu.ivos.user.pojo.dto.UserQuery;
import cn.tedu.ivos.user.pojo.dto.UserSaveParam;
import cn.tedu.ivos.user.pojo.entity.User;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public UserVO login(UserLoginParam userLoginParam) {
        log.debug("用户登录业务参数：{}",userLoginParam);
        UserVO userVO = userMapper.selectByUsername(userLoginParam.getUsername());
        if (userVO==null){
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        }
        if (!userVO.getPassword().equals(userLoginParam.getPassword())){
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }
        log.debug("用户查询结果: {}",userVO);
        return userVO;
    }

    @Override
    public void save(UserSaveParam userSaveParam) {
        log.debug("保存用户业务: {}",userSaveParam);
        User user = new User();
        BeanUtils.copyProperties(userSaveParam,user);
        log.debug("是戳: {}",user);
        if (user.getId()==null){
            user.setCreateTime(new Date());
            user.setPassword("123456");
            userMapper.insert(user);
        }else {
            user.setUpdateTime(new Date());
            userMapper.update(user);
        }

    }

    @Override
    public List<UserVO> selectUser(UserQuery userQuery) {
        log.debug("查询用户业务参数: {}",userQuery);
        List<UserVO> select = userMapper.select(userQuery);
        return select;

    }

    @Override
    public void rsetpassword(long id) {
        log.debug("重置密码业务参数: {}",id);
        User user = new User();
        user.setUpdateTime(new Date());
        user.setPassword("root");
        user.setId(id);
        userMapper.update(user);
    }

    @Override
    public void updateStatus(Long userId,String status) {
        log.debug("修改用户状态业务: {},{}",userId,status);
        User user = new User();
        user.setId(userId);
        user.setStatus(status);
        user.setUpdateTime(new Date());
        userMapper.update(user);
    }

    @Override
    public void deletUser(Long id) {
        log.debug("删除用户: {}",id);
        userMapper.delectUser(id);
    }

    @Override
    public List<UserVO> selectParentUser(Long parentId) {
        log.debug("查询直属领导业务: {}",parentId);
        List<UserVO> list = new ArrayList<>();
        UserVO userVO = userMapper.selectAudit(parentId);
        list.add(userVO);
        Long parentId1 = userVO.getParentId();
        log.debug("结果0: {}", parentId1);
        if (parentId1==null){
            return list;
        }else {
            UserVO userVO1 = userMapper.selectAudit(parentId1);
            log.debug("结果1: {}", userVO1);
            list.add(userVO1);
            return list;
        }


    }
}
